package cn.kgc.crowdfundingadminspringboot02.controller;

import cn.kgc.crowdfundingadminspringboot02.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot02.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot02.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot02.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;

    @RequestMapping("/to/login/page.html")
    public String toLoginPage() {
        return "admin-login";
    }

    //处理登录
    @RequestMapping("/do/login")
    public String doLogin(@RequestParam("loginAcct")String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession session, Model model){
        Admin admin=adminService.login(loginAcct,userPswd);
        if(admin==null){
           //登录失败
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        return "redirect:/admin/to/main/page.html";
    }

    //跳转后台主页
    @RequestMapping("/to/main/page.html")
    public String toMain(){
        return "admin-main";
    }

   //查询用户
    @RequestMapping("/query/for/search")
    public String queryForSearch( @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
                                  @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
                                  @RequestParam(value="keyword", defaultValue="") String keyword,
                                  Model model){
        PageInfo<Admin> pageInfo=adminService.queryForKeywordSearch(pageNum,pageSize,keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        return "admin_page";
    }
    //删除
    @ResponseBody
    @RequestMapping("/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        try {
            adminService.batchRemove(adminIdList);
            return ResultEntity.successWithoutData();
        }catch(Exception e) {
            return ResultEntity.failed(e.getMessage());
        }
    }
    @RequestMapping("/to/add/page.html")
    public String Toadd(HttpSession session) {
        return "admin-add";
    }
    //退出
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }

    //异常处理
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception.getMessage());
        return "system-error";
    }
    //添加用户
    @RequestMapping("/save")
    public String saveAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }

    //获取用户信息，跳转修改页面
    @RequestMapping("/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);
        return "admin-edit";
    }
    //修改
    @RequestMapping("/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }
}
